package com.view.adminMenu;

import com.run.UserRun;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.SQLException;

public class deleteUser {
    private JLabel info = new JLabel("请填写需删除用户的信息:");
    private JLabel usernameLabel = new JLabel("账号:");
    private JLabel passwordLabel = new JLabel("密码:");
    private JLabel identityLabel = new JLabel("身份:");
    private JTextField usernameField = new JTextField(20);
    private JTextField passwordField = new JTextField(20);
    private JRadioButton identity1 = new JRadioButton("user");
    private JRadioButton identity2 = new JRadioButton("manager");
    private ButtonGroup group = new ButtonGroup();
    private JButton button = new JButton("确定");
    private Box usernameBox = Box.createHorizontalBox();
    private Box passwordBox = Box.createHorizontalBox();
    private Box identityBox = Box.createHorizontalBox();
    private Box mainBox = Box.createVerticalBox();

    public JPanel init() {
        JPanel mainPanel = new JPanel();

        group.add(identity1);
        group.add(identity2);

        usernameBox.add(usernameLabel);
        usernameBox.add(Box.createHorizontalStrut(20));
        usernameBox.add(usernameField);

        passwordBox.add(passwordLabel);
        passwordBox.add(Box.createHorizontalStrut(20));
        passwordBox.add(passwordField);

        identityBox.add(identityLabel);
        identityBox.add(Box.createHorizontalStrut(20));
        identityBox.add(identity1);
        identityBox.add(identity2);

        mainBox.add(Box.createVerticalStrut(100));
        mainBox.add(info);
        mainBox.add(Box.createVerticalStrut(50));
        mainBox.add(usernameBox);
        mainBox.add(Box.createVerticalStrut(50));
        mainBox.add(passwordBox);
        mainBox.add(Box.createVerticalStrut(50));
        mainBox.add(identityBox);
        mainBox.add(Box.createVerticalStrut(50));
        mainBox.add(button);

        mainPanel.add(mainBox);

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String newUsername = usernameField.getText();
                String newPassword = passwordField.getText();
                String newIdentity = null;
                if(identity1.isSelected()) {
                    newIdentity = "user";
                }
                else if(identity2.isSelected()) {
                    newIdentity = "manager";
                }
                Connection conn = null;
                try {
                    conn = com.util.DBUtil.getConnection();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                try {
                    Boolean isDeleted = new UserRun().deleteUser(conn,newUsername,newPassword,newIdentity);
                    if(isDeleted == true) {
                        JOptionPane.showMessageDialog(null,"用户已删除");
                    }
                    else {
                        JOptionPane.showMessageDialog(null,"用户删除失败");
                    }
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
        });

        return mainPanel;
    }
}
